<?php

class Oauth_login extends CI_Model {//处理提交的url和转换淘宝客相关的关系

    const Statustrue = True;
    const Statusflase = False;

    function __construct() {
        parent::__construct();
        $this->load->helper('fuc');
        $this->load->library('topclass');
        $this->load->library('session_db');
        $this->config->load('sysetting');        
        $this->load->model('users');
        $this->config->load('topapiconfig'); //加载top配置文件
        $this->appsetingarray = $this->config->item('website_oauth'); //new_app
    }

    function action_lognin($code) {
        $appsetingarray = $this->appsetingarray;
        $grant_type = 'authorization_code';
        $redirect_uri = $appsetingarray["redirect_uri"];  //此处回调url要和后台设置的回调url相同 
        $client_id = $appsetingarray["appkey"]; //自己的APPKEYappkey
        $client_secret = $appsetingarray["appsecck"]; //自己的appsecret
        $postfields = array('grant_type' => $grant_type,
            'client_id' => $client_id,
            'client_secret' => $client_secret,
            'code' => $code,
            'redirect_uri' => $redirect_uri
        );
        $url = 'https://oauth.taobao.com/token';
        $token = json_decode(curl($url, $postfields));
        return ($token);
    }

    public function db_session($oauth_array) {//与数据库交互，存入sessiondb
        
        $this->session_db->write_Sessiondb("top_session", $oauth_array->access_token); //access_token相当于top_session
        $this->session_db->write_Sessiondb("usernick", $oauth_array->taobao_user_nick); 
       
    }

    public function db_user() {//与数据库交互，存入user表
     $this->users->user_process_oauth();
    }

}

?>
